<?php
namespace Home\Controller;
use Think\Controller;
class ListController extends CommonController {
    public function index(){
          //获取关键字
        if(!empty($_GET['keywords'])){
            //建议使用数组形式来创建where条件
            // $where = "username like '%".$_GET['keyword']."%'";
            $where['name'] = array('like','%'.$_GET["keywords"].'%');
            $cid = $_GET['keywords'];
            // $where['username'] = array('eq','admin');
            // $where['age'] = array('lt',18);
        }else{
            $cid = $_GET['cid'];
            $where['cid'] = array('eq',$cid);
            $where['fstatus'] = array('eq',1);
        }

    	// echo $cid;
    	// 创建产品模型
    	$Foods = M('mvp_foods');
        //创建优惠模型
        // $Sell = M('mvp_sell');
        $sellinfo = $Foods->field('mvp_foods.id,mvp_sell.sprice,mvp_sell.endtime')->join('left join mvp_sell on mvp_sell.fid = mvp_foods.id')->where($where )->order('id desc')->select();
        // var_dump($sellinfo);
    	$finfo = $Foods->where($where)->order('id desc')->select();
        // var_dump($finfo);die;
        
        //获取现在的时间
        $nowselltime = time();

        foreach ($finfo as $key => $value) {
            // var_dump($value);
            $finfo[$key]['score'] = ceil(array_sum($value['score'])/count($value['score']));

            $finfo[$key]['sprice'] = $sellinfo[$key]['sprice'];
            $finfo[$key]['endtime'] = $sellinfo[$key]['endtime'];
            if($nowselltime >= $sellinfo[$key]['endtime'] || empty($sellinfo[$key]['endtime'])){
                $finfo[$key]['issell'] = 0;
            }else{
                $finfo[$key]['issell'] = 1;

            }
           
        }

    	//分配
        $this->assign('finfo',$finfo);
        $this->assign('cid',$cid);
    	$this->assign('islogin',$islogin);
    	$this->display();
    }
    public function numajax(){
        $fid = $_POST['fid'];
        // echo $fid;
        //创建模型
        $Foods = M('mvp_foods');
        //修改
        $finfo = $Foods->query("update mvp_foods set fnumber = fnumber+1 where(id='{$fid}')");
        // echo $Foods->_sql();
    }    
    public function lnumajax(){
        $fid = $_POST['fid'];
        $uid = $_POST['uid'];
        // echo $fid.$lnumber;die;
        if($uid == 'true') return false;
        //创建模型

        $Logs = M('mvp_logs');
        //查询这个表里有没有这个用户
        $isuid = $Logs->where('uid='.$uid)->select();
        // var_dump($islogs);
        //如果没有则直接添加
        if(empty($isuid)){
            $_POST['lnumber'] = 1;
            $Logs->create();
            $Logs->add();
        }else{
            //如果不为空再查食物有没有
            $where['uid'] = $uid;
            $where['fid'] = $fid;
            $isfoods = $Logs->where($where)->select();
            // var_dump($isfoods);
            //如果为空则直接添加
            if(empty($isfoods)){
                $_POST['lnumber'] = 1;
                $Logs->create();
                $Logs->add();
            }else{
                //如果不为空则修改浏览次数
                $Logs->query("update mvp_logs set lnumber = lnumber+1 where(uid='{$uid}' and fid='{$fid}')");
            }
        }
        
     
    }
}
